package com.cskaoyan.service.wx.nj;

import com.cskaoyan.bean.wx.nj.CskaoyanmallFootprint;
import com.cskaoyan.bean.wx.nj.CskaoyanmallFootprintExample;
import com.cskaoyan.bean.wx.nj.CskaoyanmallGoods;
import com.cskaoyan.mapper.wx.nj.CskaoyanmallFootprintMapper;
import com.cskaoyan.mapper.wx.nj.CskaoyanmallGoodsMapper;
import com.cskaoyan.vo.wx.nj.FootprintListBean;
import com.cskaoyan.vo.wx.nj.WXFootVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class CskaoyanmallFootprintServiceImpl implements CskaoyanmallFootprintService{

    @Resource
    private CskaoyanmallFootprintMapper cskaoyanmallFootprintMapper;

    @Autowired
    CskaoyanmallGoodsMapper cskaoyanmallGoodsMapper;


    @Override
    public int deleteByExample(CskaoyanmallFootprintExample example) {
        return cskaoyanmallFootprintMapper.deleteByExample(example);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return cskaoyanmallFootprintMapper.deleteByPrimaryKey(id);
    }


    @Override
    public WXFootVo query(Integer page, Integer size, Integer userid) {
        PageHelper.startPage(page, size);

        List<CskaoyanmallFootprint> cskaoyanmallFootprints = cskaoyanmallFootprintMapper.selectAllByUserId(userid);

        List<FootprintListBean> footprintListBeanList = new ArrayList<>();
        for (int i=0;i<cskaoyanmallFootprints.size();i++){
            CskaoyanmallGoods cskaoyanmallGoods = cskaoyanmallGoodsMapper.selectByPrimaryKey(cskaoyanmallFootprints.get(i).getGoodsId());
            FootprintListBean footprintListBean = new FootprintListBean();
            footprintListBean.setBrief(cskaoyanmallGoods.getBrief());
            footprintListBean.setPicUrl(cskaoyanmallGoods.getPicUrl());
            footprintListBean.setAddTime(cskaoyanmallGoods.getAddTime().toString());
            footprintListBean.setGoodsId(cskaoyanmallGoods.getGoodsSn());
            footprintListBean.setName(cskaoyanmallGoods.getName());
            footprintListBean.setId(cskaoyanmallGoods.getId());
            footprintListBean.setRetailPrice(cskaoyanmallGoods.getRetailPrice().intValue());
        }


        WXFootVo wxFootVo  = new WXFootVo();
        wxFootVo.setFootprintList(footprintListBeanList);
        PageInfo<FootprintListBean> pageInfo = new PageInfo<>(footprintListBeanList);
        long total = pageInfo.getTotal();
        double num = (double) total / size;
        wxFootVo.setTotalPages((int)num+1);
        return wxFootVo;
    }

}
